<?php include('header.php');?>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span 12">
			<h1>ADMIN</h1>
		</div>
	</div>
	<div class="row-fluid">
		<div class="span2"">
			<?php include('lefter.php');?>
		</div>
		<div class="span10">
			<div class="row-fluid">
				<div class="span8">
					<h3>
						<span>COVER</span>
					</h3>
				</div>
				<div class="span4">
					<div class="btn-group btn-limit pull-right" data-toggle="buttons-radio">
						<button type="button" class="btn<?php if($limit == 10) :?> active<?php endif;?>">
							<i class="icon-resize-vertical"></i>10
						</button>
						<button type="button" class="btn<?php if($limit == 20) :?> active<?php endif;?>">
							<i class="icon-resize-vertical"></i>20
						</button>
						<button type="button" class="btn<?php if($limit == 50) :?> active<?php endif;?>">
							<i class="icon-resize-vertical"></i>50
						</button>
					</div>
				</div>
			</div>
			<hr />
			<div class="ul-table cover">
				<ul class="header">
					<li class="no">NO</li>
					<li class="thumb hidden-phone">THUMB</li>
					<li class="date hidden-phone">DATE</li>
					<li class="title">TITLE</li>
					<li class="imgnum hidden-phone">IMGNUM</li>
					<li class="delete">DELETE</li>
				</ul>
			</div>
			<div class="ul-table ul-table-body cover sortable">
			<?php foreach($cover_rows as $row) :?>
				<ul class="image-view">
					<li class="no"><?=$row['id']?></li>
					<li class="thumb hidden-phone"><?php if(empty($row['image_is_image'])) :?><div class="thumb-text"><?=$row['image_orig_name']?></div><?php else :?><div class="thumb-image"><img src="<?=UPLOAD_DIR?>/<?=$row['image_file_name']?>" /></div><?php endif;?></li>
					<li class="date hidden-phone"><?=preg_replace('/\-/', '.', $row['date'])?></li>
					<li class="title"><a href="<?=base_url('admin/cover_edit/'.$row['id'].'/'.$limit.'/'.$offset)?>"><?=$row['title']?></a></li>
					<li class="imgnum hidden-phone"><?=$row['image_count']?></li>
					<li class="delete"><button type="button" class="btn btn-small btn-danger btn-delete">Delete</button></li>
				</ul>
			<?php endforeach;?>
			</div>
			<div class="pagination pagination-centered">
				<ul>
					<?=$pagination_links?>
				</ul>
			</div>
			<div class="row-fluid">
				<div class="span4 offset8">
					<div class="pull-right">
					<button type="button" class="btn btn-sort hidden-phone">Save sort</button>
					<button type="button" class="btn btn-primary btn-write">Write</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script src="<?=JS_DIR?>/jquery-1.8.3.min.js"></script>
<script src="<?=JS_DIR?>/respond.min.js"></script>
<script src="<?=BS_DIR?>/admin/js/bootstrap.min.js"></script>
<script src="<?=JU_DIR?>/js/jquery-ui-1.9.2.custom.min.js"></script>
<script>
var sort_array = [<?=$sort_string?>];

$(function() {
	$("div.btn-limit button").click(function() {
		location.href = "<?=base_url(CONTROLLER.'/'.METHOD)?>/"+$(this).text()+"/";
	});
	$("div.sortable").sortable();
    $("div.sortable").disableSelection();
	$("button.btn-write").click(function() {
		location.href = "<?=base_url('admin/cover_write/'.$limit.'/'.$offset)?>";
	});
	$("button.btn-sort").click(function() {
		var id_sort_array = [];
		var div_ul_object = $("div.sortable").children("ul");
		i = 0;
		$.each(div_ul_object, function() {
			id_sort_array.push({id: $(this).children("li.no").text(), sort: sort_array[i]});
			i++;
		});
		$.ajax({
			type: 'post',
			url: "<?=base_url('admin/cover_sort_action')?>",
			data: { 
				id_sort_array: id_sort_array
			},
			success: function(data){
				alert('Sort saved!');
			},
			error: function(jqXHR, textStatus, errorThrown){
				alert( errorThrown );
			}
		});
    });
	$("button.btn-delete").click(function() {
		if(confirm('Delete?')) {
			var id = $(this).parent().parent().children("li.no").text();
			$.ajax({
				type: 'post',
				url: "<?=base_url('admin/cover_delete_action')?>",
				data: { 
					id: id
				},
				success: function(data){
					location.href = "<?=base_url('admin/cover/'.$limit.'/'.$offset)?>";
				},
				error: function(jqXHR, textStatus, errorThrown){
					alert( errorThrown );
				}
			});
		}
	});
});
</script>
<?php include('footer.php');?>